package com.example.dellpc.myapplication;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.adapter.PhotoVierPagerAdapter;
import com.example.utils.PictureUtil;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;

import me.iwf.photopicker.PhotoPickerActivity;
import me.iwf.photopicker.utils.PhotoPickerIntent;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * Created by 刘智强 on 2016/8/23.
 */
public class PhotoViewActivity extends Activity {
    //    @ViewInject(R.id.photoview)
//    private PhotoView photoView;
    private PhotoViewAttacher photoViewAttacher;
    @ViewInject(R.id.btn)
    private Button btn;
    @ViewInject(R.id.viewpager)
    private ViewPager viewPager;
    private PhotoVierPagerAdapter photoVierPagerAdapter;
    @ViewInject(R.id.tv_index)
    private TextView tv_index;

    private static final int PHOTO_REQUEST_GALLERY = 1;
    private static final int PHOTO_REQUEST_MUtIL = 2;
    private List<String> photos = new ArrayList<>();
    private List<View> views = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photoviewactivity);
        ViewUtils.inject(this);
        viewPager.setOffscreenPageLimit(0);//不预加载
        photoVierPagerAdapter = new PhotoVierPagerAdapter(getApplicationContext(), views);
        viewPager.setAdapter(photoVierPagerAdapter);
    }

    public void init() {

        new AlertDialog.Builder(this).setTitle("提示").setPositiveButton("选择照片", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(intent, PHOTO_REQUEST_GALLERY);

            }
        }).setNeutralButton("选取多张图片", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                /**
                 * 跳转到多选界面
                 */
                PhotoPickerIntent intent = new PhotoPickerIntent(getApplicationContext());
                intent.setPhotoCount(5);
                intent.setShowCamera(true);
                startActivityForResult(intent, PHOTO_REQUEST_MUtIL);
            }
        }).show();

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                tv_index.setText(position + 1 + "/" + photos.size());


            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }

    @OnClick(R.id.btn)
    public void onClickLitsenter(Button btn) {
        switch (btn.getId()) {

            case R.id.btn:
                init();
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {


        if (requestCode == PHOTO_REQUEST_GALLERY && resultCode == RESULT_OK) {

            if (data != null) {
                Uri uri = data.getData();
                String filePath = PictureUtil.uri2filePath(uri, this);
                Log.d("TAG",filePath);
                views.clear();
                viewPager.removeAllViews();
                photos.add(filePath);
                if (photos.size() > 0) {
                    for (int i = 0; i < photos.size(); i++) {
                        PhotoView photoView = new PhotoView(this);
                        photoView.setScaleType(ImageView.ScaleType.FIT_XY);
                        SoftReference<Bitmap> bitmap = new SoftReference<Bitmap>(PictureUtil.getSmallBitmap(photos.get(i), 400, 400));
                        photoView.setImageBitmap(bitmap.get());
                        views.add(photoView);
                    }
                }
                tv_index.setText("1/" + photos.size());
                photoVierPagerAdapter.notifyDataSetChanged();
            }

        } else if (requestCode == PHOTO_REQUEST_MUtIL && resultCode == RESULT_OK) {
            if (data != null) {
                List<String> selectPhotos = data.getStringArrayListExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS);
                views.clear();
                viewPager.removeAllViews();
                if (selectPhotos.size() > 0) {
                    tv_index.setVisibility(View.VISIBLE);
                    photos.addAll(selectPhotos);

                    if (photos.size() > 0) {
                        for (int i = 0; i < photos.size(); i++) {
                            PhotoView photoView = new PhotoView(this);
                            photoView.setScaleType(ImageView.ScaleType.FIT_XY);
                            SoftReference<Bitmap> bitmap = new SoftReference<Bitmap>(PictureUtil.getSmallBitmap(photos.get(i), 400, 400));
                            photoView.setImageBitmap(bitmap.get());
                            views.add(photoView);
                        }
                    }
                    tv_index.setText("1/" + photos.size());
                    photoVierPagerAdapter.notifyDataSetChanged();
                } else {
                    tv_index.setVisibility(View.GONE);
                }
            }
        }

    }
}
